package com.robusta.cityuate.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.format.DateFormat;
import android.util.Log;

public class TimeUtils {

	public static final String DATE_TIME_FORMAT = "MMM dd, yyyy kk:mm";
	public static final String DATE_FORMAT = "MMM dd, yyyy";
	public static final String TIME_FORMAT = "kk:mm";
	public static final String IN_DATE_TIME_FORMAT = DATE_FORMAT + "'T'kk:mm";

	/**
	 * parses the Date string of form yyyy-MM-ddTHH:mm:ssz
	 * (2012-11-24T15:50:40+10:00) to Date object
	 * 
	 * @param dateString
	 * @return
	 */
	// public static Date getDateTimeFromString(String dateString) {
	// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
	// Date date = null;
	// try {
	// date = format.parse(dateString);
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// return date;
	// }

	/**
	 * parses the Date string of form yyyy-MM-dd (2012-11-24) to Date object
	 * 
	 * @param dateString
	 * @return
	 */
	// public static Date getDateFromString(String dateString) {
	// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	// Date date = null;
	// try {
	// date = format.parse(dateString);
	// } catch (ParseException e) {
	// e.printStackTrace();
	// }
	// return date;
	// }

	/**
	 * Generate calendar object from dateString with specific format
	 * formatString
	 * 
	 * @param formatString
	 * @param dateString
	 * @return
	 */
	public static Calendar getCalendarFromFormat(String dateString,
			String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		Date date = null;
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			Log.e("Format", formatString);
			e.printStackTrace();
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		return c;
	}

	/**
	 * Change date format to new one
	 * 
	 * @param dateString
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 */
	public static String changeDateFormat(String dateString, String oldFormat,
			String newFormat) {
		SimpleDateFormat format = new SimpleDateFormat(oldFormat);
		Date date = null;
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long dtDob = date.getTime();
		String strDate = (DateFormat.format(newFormat, dtDob)).toString();

		return strDate;
	}

	/**
	 * format date in certain given format
	 * 
	 * @param date
	 * @param fromat
	 * @return
	 */
	public static String formatDate(Date date, String fromat) {
		// long dtDob = date.getTime();
		// return (DateFormat.format(fromat, dtDob)).toString();
		SimpleDateFormat df = new SimpleDateFormat(fromat);

		return df.format(date);
	}

	/**
	 * Create date object from date string and format of the string
	 * 
	 * @param dateString
	 * @param formatString
	 * @return
	 */
	public static Date getDateTimeFromString(String dateString,
			String formatString) {
		SimpleDateFormat format = new SimpleDateFormat(formatString);
		Date date = null;
		try {
			date = format.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * calculate the difference between some time and current time
	 * 
	 * @param time
	 * @return the time difference in for x (days/hours/minutes/seconds)
	 */
	public static String getTimeFromNow(Calendar time) {

		long difMilli = time.getTimeInMillis()
				- Calendar.getInstance().getTimeInMillis();

		int days = (int) difMilli / (1000 * 60 * 60 * 24);
		if (days > 0)
			return (days == 1) ? "Yesterday" : (days + " days");

		int hours = (int) difMilli / (1000 * 60 * 60);
		if (hours > 0)
			return (hours == 1) ? "1 hour" : (hours + " hours");

		int minutes = (int) difMilli / (1000 * 60);
		if (minutes > 0)
			return (minutes == 1) ? "1 minute" : (minutes + " minutes");

		int seconds = (int) difMilli / (1000 * 60);
		if (seconds > 0)
			return (seconds == 1) ? "1 second" : (seconds + " seconds");

		return "less than a second";

	}

	public static CharSequence formatDateFromMillis(long milis, String string) {

		String hours = "" + milis / 3600000;
		hours = (hours.length() == 1) ? ("0" + hours) : hours;
		long r = milis % 3600000;
		String minutes = "" + r / 60000;
		minutes = (minutes.length() == 1) ? ("0" + minutes) : minutes;
		r = r % 60000;
		String seconds = "" + r / 1000;
		seconds = (seconds.length() == 1) ? ("0" + seconds) : seconds;
		return hours + ":" + minutes + ":" + seconds;
	}

}
